package phoenix.web.test_objects;

import org.testng.TestException;
import phoenix.core.PhoenixManager;
import phoenix.core.base.extensions.TestBaseWebExt;
import phoenix.core.data_models.TestUserDataModel;
import phoenix.core.helpers.extensions.WebDriverHelperExt;
import phoenix.web.test_objects.data_model.NaughtyMode;
import phoenix.web.test_objects.data_model.NotificationsAlerts;
import phoenix.web.test_objects.data_providers.NaughtyModeDataProviders;

/**
 * Created by AlexGolovko on 28.11.2014.
 */
public class NaughtyModeTests extends TestBaseWebExt
{
	/**
	 * Initializes TestBaseMobExt
	 * @param p_manager PhoenixManager instance
	 */
	public NaughtyModeTests(PhoenixManager p_manager)
	{
		super(p_manager);
	}

	public NaughtyModeDataProviders dataProviders = new NaughtyModeDataProviders(manager);

	/**
	 * @param p_user User in test
	 * @param p_expectedNaughtyMode expected mode on profile
	 * @param p_defaultNaughtyMode default mode on site
	 */
	public void searchNaughtyTest(TestUserDataModel p_user, NaughtyMode p_expectedNaughtyMode, NaughtyMode p_defaultNaughtyMode)
	{
		try
		{
			// Login
			wd().openAutologin(testUser());

			if (p_defaultNaughtyMode != null)
			{
				page().naughtyModePage().setNaughtyMode(p_defaultNaughtyMode);
			}

			// Open user page
			wd().openPage("/user/view/id/" + p_user.getId());
			page().userPage().waitForPageLoad();

			// Add to favorites
			if (!page().userPage().isFriendsTextPresent())
			{
				page().userPage().clickAddFriends();
				wd().wait(2); // time for sync
			}

			// Do the test
			wd().openPage("/favorites");
			page().searchPage().waitForPageLoad();
			wd().wait(2); // Ajax load
			// Check user presence
			if (!page().searchPage().isUserPresent(p_user))
			{
				throw new TestException("User isn't found at search page " + p_user.getId());
			}

			NaughtyMode p_actualNaughtyMode = page().naughtyModePage().getTypeInSearch(p_user);
			// Test for naughty mode
			test().assertEquals(p_actualNaughtyMode, p_expectedNaughtyMode, "searchNaughtyTest: " + p_user.getId());
		}
		catch (Exception p_ex)
		{
			log().errorAndScreen("searchNaughtyTest " + p_ex.toString());
		}
		finally
		{
			test().forErrors();
		}
	}

	public void userProfileNaughtyTest(TestUserDataModel p_user, NaughtyMode p_expectedNaughtyMode, NaughtyMode p_defaultNaughtyMode)
	{
		try
		{
			// Login
			wd().openAutologin(testUser());
			if (p_defaultNaughtyMode != null)
			{
				page().naughtyModePage().setNaughtyMode(p_defaultNaughtyMode);
			}

			// Open user page
			wd().openPage("/user/view/id/" + p_user.getId());
			page().userPage().waitForPageLoad();

			NaughtyMode p_actualNaughtyMode = page().naughtyModePage().getModeTypeUserProfile();
			// Test for Naughty Mode
			test().assertEquals(p_actualNaughtyMode, p_expectedNaughtyMode, "userProfileNaughtyTest: " + p_user.getId());
		}
		catch (Exception p_ex)
		{
			log().errorAndScreen("userProfileNaughtyTest " + p_ex.toString());
		}
		finally
		{
			test().forErrors();
		}
	}

	public void galleryNaughtyTest(TestUserDataModel p_user, NaughtyMode p_expectedNaughtyMode, NaughtyMode p_defaultNaughtyMode)
	{
		try
		{
			wd().openAutologin(testUser());
			if (p_defaultNaughtyMode != null)
			{
				page().naughtyModePage().setNaughtyMode(p_defaultNaughtyMode);
			}

			wd().openUserProfile(p_user);
			page().galleryPage().clickUserPhoto();
			page().galleryPage().waitForPageLoad();
			page().galleryPage().clickAllPhotos();

			// Test Naughty Mode on Main photo
			NaughtyMode p_actualNaughtyModeMainPhoto = page().naughtyModePage().getTypeMainPhoto();
			test().assertEquals(p_actualNaughtyModeMainPhoto, p_expectedNaughtyMode, "galleryNaughtyTest: MainPhoto type " + p_user.getId());

			NaughtyMode p_actualNaughtyModeGalleryRoll = page().naughtyModePage().getTypeOfGalleryRoll();
			test().assertEquals(p_actualNaughtyModeGalleryRoll, p_expectedNaughtyMode, "galleryNaughtyTest: Gallery roll type " + p_user.getId());
		}
		catch (Exception p_ex)
		{
			log().errorAndScreen("galleryNaughtyTest " + p_ex.toString());
		}
		finally
		{
			test().forErrors();
		}
	}

	public void notificationsNaughtyTest(TestUserDataModel p_user, NaughtyMode p_expectedNaughtyMode, NaughtyMode p_defaultNaughtyMode)
	{
		try
		{
			WebDriverHelperExt.driverHandles = wd().initSecondBrowser();
			// Login by recipient
			wd().getSecondWindow();
			//
			manager.wd().openAutologin(testUser());
			// Preparation steps
			manager.wd().openPage("/account");
			NotificationsAlerts params = new NotificationsAlerts();
			params.disableAllPopUps();
			params.browse = true; // Only browse is enabled
			manager.backend().notificationsSettingsPage().setNotifications(manager.testUser(), params);
			manager.web().myAccountPage().disableMatchNotifications(testUser()); // Disable all match notifications
			// Set default mode
			if (p_defaultNaughtyMode != null)
			{
				page().naughtyModePage().setNaughtyMode(p_defaultNaughtyMode);
			}

			// Do action by sender
			wd().getPrimaryWindow();
			manager.wd().openAutologin(p_user);
			wd().openUserProfile(p_user, testUser());

			// Test Begins
			wd().getSecondWindow();

			Integer tries = 0;
			while (!page().searchPage().isPopUpAlertVisible())
			{
				wd().wait(30); // Wait 10 sec
				tries++;
				if (tries == 6)
				{
					throw new TestException("Unable to find pop-up, after 3 minutes");
				}
			}

			NaughtyMode p_actualMode = page().naughtyModePage().getModeTypeNotification();
			test().assertEquals(p_actualMode, p_expectedNaughtyMode, "notificationsNaughtyTest: " + p_user.getId());
			page().searchPage().closePopUpAlert();

		}
		catch (Exception p_ex)
		{
			log().errorAndScreen("notificationsNaughtyTest " + p_ex.toString());
		}
		finally
		{
			wd().getSecondWindow();
			wd().close();
			wd().getPrimaryWindow();
			test().forErrors();
		}
	}

	public void switcherNaughtyTest(TestUserDataModel p_user, NaughtyMode p_testMode)
	{
		try
		{
			// Sexy mode
			wd().openAutologin(testUser());
			// Search test
			this.searchNaughtyTest(p_user, NaughtyMode.NORMAL, p_testMode);
			this.userProfileNaughtyTest(p_user, NaughtyMode.NORMAL, p_testMode);
			this.galleryNaughtyTest(p_user, NaughtyMode.NORMAL, p_testMode);
			this.notificationsNaughtyTest(p_user, NaughtyMode.NORMAL, p_testMode);
		}
		catch (Exception p_ex)
		{
			log().errorAndScreen("switcherNaughtyTest " + p_ex.toString());
		}
		finally
		{
			test().forErrors();
		}
	}
}
